﻿using DouYinFiddlerPlugin;
using Fiddler;
using SpeechLib;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

public class MaiCheSettingView : IFiddlerExtension
{

   public UserControl1 view = null;
    SocketUtils socketUtils = null;
    public static bool ok = false;
    public static Process pro = new Process();


    public void speak(string txt)
    {
        SpVoice voice = new SpVoice();
        voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0);
        voice.Speak(txt, SpeechVoiceSpeakFlags.SVSFDefault);
    }


    public void OnLoad()
    {


        string path = System.Environment.CurrentDirectory + @"\Scripts\decoding.exe";
        if (!File.Exists(path))
        {
            MessageBox.Show(path + "请检查decoding.exe 是否存在!或被杀软屏蔽! 尝试重启程序!");
            return;
        }

        view = new UserControl1();
        view.Dock = DockStyle.Fill;
        var tab = new TabPage();
        tab.Text = "抖音弹幕朗读插件";
        tab.Controls.Add(view);
        FiddlerApplication.UI.tabsViews.TabPages.Add(tab);
        view.Click += usercontrol_Click;
        FiddlerApplication.BeforeResponse += FiddlerApplication_BeforeResponse;

        SpVoice voice = new SpVoice();
        voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0);

        Task.Run(() => {
            speak("欢迎使用弹幕助手!本插件免费!请勿为此付费!欢迎关注我的博客:https://destiny.cool");
        });

        socketUtils = new SocketUtils();
        socketUtils.userControl1 = view;

        startTask(path);


       // socketUtils.Send(Encoding.UTF8.GetBytes("1234567890"));//发送数据
    }



    public void startTask(string path)
    {
        Task.Run(() => {

            while (true)
            {
                START:
                try
                {
                    ProcessStartInfo info = new ProcessStartInfo();
                    info.FileName = path;
                    info.Arguments = "0.0.0.0 9200";
                  
                    info.WindowStyle = ProcessWindowStyle.Normal;
                    //启动程序
                    Process.Start(info);
                    



                    ok = true;

                }
                catch (Exception ex)
                {
                    speak("启动程序失败!");
                    break;
                }


                Thread.Sleep(1000);
                if (ok)
                {

                    socketUtils.Connect();
                    break;
                }
                else
                {
                    view.addLog("尝试启动decoding.exe");
                    goto START;
                }
            }
        });


    }

    private void FiddlerApplication_BeforeResponse(Session oSession)
    {

        if (oSession.fullUrl.IndexOf("https://live.douyin.com/webcast/im/fetch/") <= -1)
        {
            return;
        }

        oSession.utilDecodeResponse();

        socketUtils.Send(oSession.responseBodyBytes);

        // throw new NotImplementedException();
    }

    public void OnBeforeUnload()
    {
        //throw new System.NotImplementedException();
    }


    private void usercontrol_Click(object sender, EventArgs e)
    {
     //   MessageBox.Show("TEST", "TEST");
    }



 












}

